const mongoose = require("mongoose");

const mongooseDao = require("./mongooseDao");
const AlbumModel = mongooseDao.model.albumModel;
const MusicModel = mongooseDao.model.musicModel;
const Page = require("../utils/pageUtil");

const albumService = function(){

	/**
	 * 查询专辑列表
	 * @param query - 查询条件
	 * @param response - HTTP请求的响应对象
	 * @param view - 视图名称
	 */
	this.dir = function(query, response, view) {
		var pageConfig = query.page;
		var page = new Page(pageConfig);
		AlbumModel.count(query.condition).exec(function(err, count){
			page.setCount(count);
			AlbumModel
				.find(query.condition)
				.skip(page.rowStart)
				.limit(page.pageSize)
				.sort({publishTime : -1}) //按照发布时间降序排列
				.exec(function(err, docs){
					response.render(view, {albums : docs,page : page});
				});
		});
	};
	/**
	 * 获取一个专辑的详情
	 * @param albumId - 专辑的ID
	 * @param response - HTTP请求的响应对象
	 * @param view - 视图名称
	 */
	this.get = function(albumId, response, view) {
		MusicModel.find({albumId :albumId}).exec(function(err, docs){
			response.render(view, {musics : docs});
		});
	};
	/**
	 * 保存专辑
	 * @param album - 使用实体对象
	 */
	this.save = function(album) {
		const albumModel = new AlbumModel(album);
		albumModel.save();
	};
	/**
	 * 删除专辑
	 * @param albumId - 用户ID
	 */
	this.del = function(albumId) {
		//TODO 删除专辑

	}
};
module.exports = new albumService();